﻿using huqiang.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

public class AssetBundleManager
{
    class DESEncryptTask
    {
        MemoryStream memory = new MemoryStream();
        DESEncrypt des = new DESEncrypt("0x86-130", "48978309");
        public AssetBundleMission _mission;
        public bool running;
        public bool done;
        public async void Run(AssetBundleMission mission)
        {
            done = false;
            running = true;
            bool ok = false;
            _mission = mission;
            await Task.Run(() => {
                try
                {
                    des.DecryptMS(File.ReadAllBytes(mission.FilePath), memory);
                    memory.Seek(0, SeekOrigin.Begin);
                    ok = true;
                }
                catch (Exception ex)
                {
                    UnityEngine.Debug.LogError(ex.StackTrace);
                }
            });
            if (ok)
            {
                UnityEngine.AssetBundleCreateRequest abcr = UnityEngine.AssetBundle.LoadFromStreamAsync(memory);
                abcr.completed += (o) => {
                    running = false;
                    _mission.assetBundle = abcr.assetBundle;
                    done = true;
                };
            }
            else
            {
                running = false;
                done = true;
            }
        }
    }
    static List<AssetBundleMission> missions = new List<AssetBundleMission>();
    /// <summary>
    /// 同时进行的最大任务数量
    /// </summary>
    static int MaxTask = 8;
    static DESEncryptTask[] tasks;
    public static void Initial(int taskCount = 8)
    {
        MaxTask = taskCount;
        if (MaxTask < 0)
            MaxTask = 1;
        tasks = new DESEncryptTask[MaxTask];
        for (int i = 0; i < MaxTask; i++)
        {
            tasks[i] = new DESEncryptTask();
        }
    }
    public static void Update()
    {
        int mt = MaxTask;
        if (mt <= 0)
            mt = 1;
        for (int i = 0; i < MaxTask; i++)
        {
            if (tasks[i].done)
            {
                try
                {
                    if (tasks[i]._mission.Completed != null)
                        tasks[i]._mission.Completed(tasks[i]._mission);
                }
                catch (Exception ex)
                {
                    UnityEngine.Debug.LogError(ex.StackTrace);
                }
            }
            if (!tasks[i].running)
            {
                if (missions.Count > 0)
                {
                    var miss = missions[0];
                    missions.RemoveAt(0);
                    tasks[i].Run(miss);
                }
            }
        }
    }
    public static UnityEngine.AssetBundle LoadFromFile(string path)
    {
        return null;
    }
    public static AssetBundleMission LoadFromFileAsync(string path)
    {
        for (int i = 0; i < missions.Count; i++)
        {
            if (missions[i].FilePath == path)
                return missions[i];
        }
        var abm = new AssetBundleMission();
        abm.FilePath = path;
        missions.Add(abm);
        return abm;
    }
}

public class AssetBundleMission:IMission
{
    public string FilePath;
    public UnityEngine.AssetBundle assetBundle;
}